package application.manager;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 自动注入管理者
 */
public class InjectionManager {

    private static InjectionManager injectionManager = new InjectionManager();

    @Getter
    private Map<Class<?>, Object> injections;

    private InjectionManager() {
        injections = new HashMap<>();
    }

    public static InjectionManager getInstance() {
        if (injectionManager == null) {
            injectionManager = new InjectionManager();
        }
        return injectionManager;
    }

    public void register(Class<?> cls, Object o) {
        this.injections.put(cls, o);
    }

    public void unregister(Class<?> cls) {
        boolean b = this.injections.containsKey(cls);
        if (b) {
            this.injections.remove(cls);
        }
    }

    public <T> T lookup(Class<T> cls) {
        return (T) this.injections.get(cls);
    }
}
